package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import module.CusProgress;
import module.NewHouse;
import module.RequirementHouse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import utils.PageUtil;
import bussinessService.basicbussinessService;
import bussinessService.impl.CusProgressBussinessServiceImpl;
import bussinessService.impl.NewHouseBussinessServiceImpl;
import bussinessService.impl.RequirementHouseBussinessServiceImpl;
import domain.vo.CusProgressQueryObject;
import domain.vo.CusProgressVO;
import domain.vo.JSONObject;
import domain.vo.Message;
import domain.vo.NewHouseVO;
import domain.vo.RequirementHousVO;

/**
 * 
* @ClassName: CusProcessController 
* @Description: 跟进MVC
* @author chenchao
* @date 2014年1月22日 上午10:16:29 
*
 */
@Controller
@RequestMapping("/cusProgress")
public class CusProgressController {
	private Logger logger = Logger.getLogger(CusProgressController.class.getName());
	/**
	 * 日志业务层
	 */
	private basicbussinessService<CusProgress,CusProgressVO> cusProcessBIS = new CusProgressBussinessServiceImpl();
	/**
	 * 房屋业务层实现类
	 */
	private basicbussinessService<NewHouse,NewHouseVO> houseBIS = new NewHouseBussinessServiceImpl();
	/**
	 * 客源
	 */
	private basicbussinessService<RequirementHouse, RequirementHousVO> rhBSI = new RequirementHouseBussinessServiceImpl();
	
	@RequestMapping("/viewCusProgressJson")
	public @ResponseBody
	JSONObject<CusProgressVO> viewListJson(
			@RequestParam("page") int page, @RequestParam("rows") int rows,CusProgressQueryObject queryObject ,HttpServletRequest request) {
		JSONObject<CusProgressVO> json = new JSONObject<CusProgressVO>();
		List<CusProgressVO> vos = new ArrayList<CusProgressVO>();
		try {
			PageUtil.Page(page, rows);
			queryObject.setStart(PageUtil.start);
			queryObject.setEnd(PageUtil.end);
			queryObject.setFkid(request.getParameter("fkid"));
			int len = cusProcessBIS.countByQueryObject(queryObject);
			vos = cusProcessBIS.getObjectByQueryObject(queryObject);
			json.setTotal(len);
			json.setRows(vos);
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}

		return json;

	}
	
	@RequestMapping("/addCusProgress")
	public @ResponseBody Message addCusProgress(CusProgress progress, HttpSession session,HttpServletRequest request) {
		Message ms = new Message();
		try {
			String e_sn = session.getAttribute("e_sn").toString();
			String e_name = session.getAttribute("e_name").toString();
			String type = request.getParameter("progressType");
			progress.setE_name(e_name);
			progress.setType(type);
			progress.setE_sn(e_sn);
			String fkId = progress.getCus_id().substring(1);
			NewHouse nh = houseBIS.getById(fkId);
			if(nh != null) {
				nh.setCreateTime(new Date());
				progress.setNewHouse(nh);
			} else {
				RequirementHouse rh = rhBSI.getById(fkId);
				if (rh != null) {
					rh.setProgressTime(new Date());
					progress.setReqHouse(rh);
				}
			}
	
			cusProcessBIS.merageObject(progress);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			ms.setErrorMsg("房屋基本数据保存失败");
		}
		
		return ms;
	}
}
